Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safer isArray function #3606

Merged
merged 4 commits into from
Aug 22, 2024
Merged

Add safer isArray function #3606

merged 4 commits into from
Aug 22, 2024

Conversation

jtran
Copy link
Collaborator

@jtran jtran commented Aug 21, 2024

I extracted this utility function from #3207 to make TypeScript isArray() checks safer by preserving the element type, not assuming they're any.

TypeScript's built-in types for Array.isArray() incorrectly uses any[]. It should use unknown[]. The consequence of using Array.isArray() is that even if you've narrowed a value to a more specific type, TS will treat it as any after the check, which effectively disables type-checking.

In this PR, as a demonstration, I only converted a few call sites until I hit one that had a cascading effect that triggered a type error near the call site. In massageFormats(), the new Vector3(a[0], a[1], a[2]) became an error since TS could no longer assume that a[i] was any. I addressed it by making the massageFormats() function parameter type more accurate, and this cascaded to the caller of that, where I added an early return.

Relevant issues:

Copy link

qa-wolf bot commented Aug 21, 2024

QA Wolf here! As you write new code it's important that your test coverage is keeping up.
Click here to request test coverage for this PR!

Copy link

vercel bot commented Aug 21, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
modeling-app ✅ Ready (Inspect) Visit Preview Aug 22, 2024 1:58pm

@jessfraz jessfraz merged commit 64500d0 into main Aug 22, 2024
19 of 24 checks passed
@jessfraz jessfraz deleted the jtran/ts-is-array branch August 22, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants